package org.bjtu.service;

import java.util.ArrayList;
import java.util.List;

import org.bjtu.model.Individualuser;
import org.bjtu.model.Memberlevel;
import org.bjtu.model.Membertype;

import com.janus.persistence.hibernate.HibernateUtil;


public class UserPermissionService extends BaseService {
	/*
	public ArrayList showUserRightList(){
	
		FunctionrightDAO right = new FunctionrightDAO();
//		ArrayList<UserRoleListPOJO> list = (ArrayList<UserRoleListPOJO>) right.showUserRightList();
		ArrayList<UserRoleListPOJO> list = (ArrayList<UserRoleListPOJO>) right.showUserRightList();
		System.out.println("list size = " + list.size());
		for(int i = 0; i < list.size(); i++){
			System.out.println(list.get(i).getMemTname());
		}
		return list;
	}
	 */
	public String[] getLevelList(){
		String hql = "select distinct l.memlname from Memberlevel l";
		List list = HibernateUtil.query(hql);
		return (String[]) list.toArray(new String[0]);
	}
	public Membertype[] getTypeList(){
		String hql = "from Membertype";
		List list = HibernateUtil.query(hql);
		return (Membertype[]) list.toArray(new Membertype[0]);
	}

	public Memberlevel[] getTypeLevelList(long typeId, String levelName)
	{
		String hql = "from Memberlevel l order by l.membertype.id where 1=1";
		if(typeId != 0){
			hql += " and l.membertype.id = " + typeId;
		}
		if(! (levelName.equals("0") )){
			hql += " and l.memlname = '" + levelName + "'";
		}
		
		List list = HibernateUtil.query(hql);
		return (Memberlevel[]) list.toArray(new Memberlevel[0]);
	}
	
	public Membertype[] getAllUserType()
	{
	    String hql = "from Membertype";
	    List list = HibernateUtil.query(hql);
	    return (Membertype[]) list.toArray(new Membertype[0]);
	}
	
	public void deleteType(long typeId){
//		String HQL = "from Memberlevel l where l.membertype.id =? ";
//		
//		Memberlevel[] list = (Memberlevel[])HibernateUtil.query(HQL,new Object[] {typeId}).toArray(new Memberlevel[0]);
//		for(int i = 0; i < list.length; i++){
//			list[i].delete();
//		}
		Membertype type = new Membertype();
		type.setId(typeId);
		try{
			type.delete();
		}
		catch(Exception e){
			System.out.println("exception");
		}
	}
	
	public void deleteLevel(long levelId){
		Memberlevel level = new Memberlevel();
		level.setId(levelId);
		level.delete();
	}
	
	public void updateType(long id, String name){
		Membertype type = this.getMembertypeById(id);
		type.setMemtname(name);
		type.saveOrUpdate();
	}
	
	public void updateLevel(long id, String name){
		Memberlevel level = this.getMemberlevelById(id);
		level.setMemlname(name);
		level.saveOrUpdate();
	}
	
	public Membertype getMembertypeById(long id)
	{
	    return (Membertype)new Membertype().getInstanceById(id);
	}
	
	public Memberlevel getMemberlevelById(long id)
	{
	    return (Memberlevel)new Memberlevel().getInstanceById(id);
	}
}
